//
//  CreditViewController.m
//  YTwallet
//
//  Created by ZhangLin on 2019/3/8.
//  Copyright © 2019年 hadrlog. All rights reserved.
//

#import "CreditViewController.h"
#import "CreditView.h"
#import <Masonry.h>
#import "AppDelegate.h"
#import "HNineCollectionViewCell.h"

#import "UITextField+ExtentRange.h"
#import "SelectCardViewController.h"

#import "GetReceiptCardModelPack.h"
#import "BankLimitViewController.h"

#define WIDTH  [[UIScreen mainScreen]bounds].size.width

#define HEIGHT [[UIScreen mainScreen]bounds].size.height

@interface CreditViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UITextFieldDelegate>
@property(nonatomic,strong)CreditView *cv;
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)UIButton *delBtn;
@property(nonatomic,strong)UIButton *payBtn;
@property(nonatomic,strong)UIButton *zeroBtn;

@property(nonatomic,strong)NSMutableArray *titleArray;

@property(nonatomic,strong)NSString* HoriginalAmount;
@property(nonatomic,strong)NSString *HnewAccount;


@end

@implementation CreditViewController

-(NSMutableArray *)titleArray
{
    if (_titleArray == nil) {
        _titleArray = [NSMutableArray array];
    }
    return _titleArray;
}

#pragma mark 初始化
-(void)loadView{
    [super loadView];
    self.cv = [[CreditView alloc] initWithFrame:self.view.bounds];
    self.view = self.cv;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.cv.collectCardValueLab.attributedText = nil;
    
    self.title = @"信用卡收款";
    self.view.backgroundColor = [UIColor whiteColor];
    [self createRightBtn];
    
    //收款卡提示
    [self.cv.collectCardBtn addTarget:self action:@selector(collectCardBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
    //创建键盘
    [self createNum];
    
    [self.payBtn addTarget:self action:@selector(payBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    //查询银行卡号,并显示后4位
    [self getReceiptCard];
}
-(void)createRightBtn
{
    UIButton *rightBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    rightBtn.tag = 10000;
    rightBtn.frame = CGRectMake(0, 0, 40, 100);
    [rightBtn addTarget:self action:@selector(rightBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [rightBtn setTitle:@"额度提醒"  forState:(UIControlStateNormal)];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    
}
-(void)rightBtnAction:(id)sender{
    BankLimitViewController *bank = [[BankLimitViewController alloc] init];
    [self.navigationController pushViewController:bank animated:NO];
}

-(void)getReceiptCard
{
    NSString *url = [[ServerAddressModel shareInstance] fullTAnd0AddrWithParameter:@"/epay_apps/user/userInfoE0100"];
    [[ETONEHttpClient sharedInstance] doHTTPSPOST:url Parameters:nil HttpBody:nil SuccessHandler:^(id responseObject) {
        
        NSDictionary *dic = [responseObject mj_JSONObject];
        NSString *code = dic[@"code"];
        if ([@"0000" isEqualToString:code]) {
            [self dealGetReceiptWith:responseObject];
        }
        
    } FailedHandler:^(NSError *error) {
        
    }];
}

-(void)dealGetReceiptWith:(id)responseObject
{
    GetReceiptCardResponse *getReceiptResponse = [GetReceiptCardResponse mj_objectWithKeyValues:responseObject];
    NSString *bankNameStr = getReceiptResponse.response.openBankName;
    NSString *banCardNumStr = [AppTools changeCardNum:getReceiptResponse.response.accountNo];
    NSString *str = [NSString stringWithFormat:@"%@(尾号%@)",bankNameStr,banCardNumStr];
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
    [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, str.length)];
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0f] range:NSMakeRange(0, bankNameStr.length)];
    
    
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:153.0f/255.0f green:153.0f/255.0f blue:153.0f/255.0f alpha:1.0f] range:NSMakeRange(bankNameStr.length, 8)];
    dispatch_async(dispatch_get_main_queue(), ^{
        self.cv.collectCardValueLab.attributedText = attributedString;
    });
}

-(void)payBtnAction
{
    NSLog(@"点击确认收款");
    NSLog(@"金额为:%.2f",[self.cv.visableTf.text floatValue]);
    NSString *str = [NSString stringWithFormat:@"%.2f",[self.cv.visableTf.text floatValue]];
    SelectCardViewController *selectCardVC = [[SelectCardViewController alloc] init];
    selectCardVC.moneyStr = str;
    [self.navigationController pushViewController:selectCardVC animated:YES];
}

-(void)createNum
{
    self.titleArray = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@".",@"0",@"00",].mutableCopy;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat with = WIDTH / 4.0;
    CGFloat higth = with - 20;
    layout.itemSize = CGSizeMake(with, higth);
    NSLog(@"%f%f",with,higth);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, HEIGHT - higth * 4 -  ETTopHeight - ETTabBarArcHeight, WIDTH - WIDTH / 4.0, higth * 4) collectionViewLayout:layout];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
//    self.collectionView.delaysContentTouches = false;
    [self.cv addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor colorWithRed:238 / 255.0 green:238 / 255.0 blue:240 / 255.0 alpha:1];
    [self.collectionView registerClass:[HNineCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    
    UIView *horizontalLineView1 = [[UIView alloc] init];
    horizontalLineView1.backgroundColor = [UIColor colorWithRed:225 / 255.0 green:225 / 255.0 blue:225 / 255.0 alpha:1];
    horizontalLineView1.frame = CGRectMake(0, HEIGHT - higth * 4 -  ETTopHeight - ETTabBarArcHeight, WIDTH, 1);
    [self.view addSubview:horizontalLineView1];
    
    
    
    UIView *horizontalLineView2 = [[UIView alloc] init];
    horizontalLineView2.backgroundColor = [UIColor colorWithRed:225 / 255.0 green:225 / 255.0 blue:225 / 255.0 alpha:1];
    horizontalLineView2.frame = CGRectMake(0, HEIGHT - higth * 4 -  ETTopHeight - ETTabBarArcHeight + higth, WIDTH - WIDTH / 4.0, 1);
    [self.view addSubview:horizontalLineView2];
    
    UIView *horizontalLineView3 = [[UIView alloc] init];
    horizontalLineView3.backgroundColor = [UIColor colorWithRed:225 / 255.0 green:225 / 255.0 blue:225 / 255.0 alpha:1];
    horizontalLineView3.frame = CGRectMake(0, HEIGHT - higth * 4 -  ETTopHeight - ETTabBarArcHeight + higth * 2, WIDTH - WIDTH / 4.0, 1);
    [self.view addSubview:horizontalLineView3];
    
    
    UIView *horizontalLineView4 = [[UIView alloc] init];
    horizontalLineView4.backgroundColor = [UIColor colorWithRed:225 / 255.0 green:225 / 255.0 blue:225 / 255.0 alpha:1];
    horizontalLineView4.frame = CGRectMake(0, HEIGHT - higth * 4 -  ETTopHeight - ETTabBarArcHeight + higth * 3, WIDTH - WIDTH / 4.0, 1);
    [self.view addSubview:horizontalLineView4];
    
    //如果是刘海屏手机最底部增加一条线
    UIView *horizontalLineView5 = [[UIView alloc] init];
    horizontalLineView5.backgroundColor = [UIColor colorWithRed:225 / 255.0 green:225 / 255.0 blue:225 / 255.0 alpha:1];
    horizontalLineView5.frame = CGRectMake(0, HEIGHT - higth * 4 -  ETTopHeight - ETTabBarArcHeight + higth * 4 - 1, WIDTH - WIDTH / 4.0, 1);
    [self.view addSubview:horizontalLineView5];
    
    
    
    UIView *verticalLineView1 = [[UIView alloc] init];
    verticalLineView1.backgroundColor = [UIColor colorWithRed:225 / 255.0 green:225 / 255.0 blue:225 / 255.0 alpha:1];
    verticalLineView1.frame = CGRectMake(WIDTH / 4.0, HEIGHT - higth * 4 -  ETTopHeight - ETTabBarArcHeight,  1, higth *4);
    [self.view addSubview:verticalLineView1];
    
    
    UIView *verticalLineView2 = [[UIView alloc] init];
    verticalLineView2.backgroundColor = [UIColor colorWithRed:225 / 255.0 green:225 / 255.0 blue:225 / 255.0 alpha:1];
    verticalLineView2.frame = CGRectMake(WIDTH / 4.0 * 2, HEIGHT - higth * 4 -  ETTopHeight - ETTabBarArcHeight,  1, higth * 4);
    [self.view addSubview:verticalLineView2];
    
    
    UIView *verticalLineView3 = [[UIView alloc] init];
    verticalLineView3.backgroundColor = [UIColor colorWithRed:225 / 255.0 green:225 / 255.0 blue:225 / 255.0 alpha:1];
    verticalLineView3.frame = CGRectMake(WIDTH / 4.0 * 3, HEIGHT - higth * 4 -  ETTopHeight - ETTabBarArcHeight,  1, higth);
    [self.view addSubview:verticalLineView3];
    
    self.delBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.delBtn setImage:[UIImage imageNamed:@"icon_revoke"] forState:(UIControlStateNormal)];
    self.delBtn.frame = CGRectMake(with * 3 + 1, HEIGHT - higth * 4 -  ETTopHeight - ETTabBarArcHeight + 1, with - 1, higth - 1);
    [self.view addSubview:self.delBtn];
    
    self.payBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.payBtn.userInteractionEnabled = NO;
    [self.payBtn setTitle:@"确认\n收款" forState:(UIControlStateNormal)];
    [self.payBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    self.payBtn.titleLabel.numberOfLines = 0;
    self.payBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    self.payBtn.backgroundColor = [UIColor colorWithRed:219 / 255.0 green:224 / 255.0 blue:255 / 255.0 alpha:1];
    self.payBtn.frame = CGRectMake(with * 3, HEIGHT - higth * 4 -  ETTopHeight - ETTabBarArcHeight + higth, with, higth * 3);
    [self.view addSubview:self.payBtn];
    
  
    [self.delBtn addTarget:self action:@selector(delBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
    [self.delBtn addTarget:self action:@selector(delBtnhighlighted:) forControlEvents:(UIControlEventTouchDown)];
    [self.delBtn addTarget:self action:@selector(delBtnNormal:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //隐藏系统键盘
    self.cv.visableTf.inputView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.cv.visableTf becomeFirstResponder];
    self.cv.visableTf.delegate = self;
    [self.cv.visableTf addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}
-(void)delBtnhighlighted:(UIButton *)sender
{
    sender.backgroundColor = ETColorWithRGB(242, 244, 255);
}
-(void)delBtnNormal:(UIButton *)sender
{
    sender.backgroundColor = [UIColor whiteColor];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.cv.visableTf becomeFirstResponder];
}

-(void)textFieldDidChange :(UITextField *)textField{
    //超过8位变小
    if (textField.text.length >= 8) {
        self.cv.visableTf.font = [UIFont systemFontOfSize:40.0];
    }
    
    if (textField.text.length < 8) {
        self.cv.visableTf.font = [UIFont systemFontOfSize:48.0];
    }
    
    if ([textField.text isEqualToString:@"0"]) {
        self.payBtn.userInteractionEnabled = NO;
        self.payBtn.backgroundColor = [UIColor colorWithRed:219 / 255.0 green:224 / 255.0 blue:255 / 255.0 alpha:1];
        return;
    }
    
    if ([textField.text isEqualToString:@"0."]) {
        self.payBtn.userInteractionEnabled = NO;
        self.payBtn.backgroundColor = [UIColor colorWithRed:219 / 255.0 green:224 / 255.0 blue:255 / 255.0 alpha:1];
        return;
    }
    
    if (textField.text.length > 0) {
        self.payBtn.userInteractionEnabled = YES;
        self.payBtn.backgroundColor = [UIColor colorWithRed:91 / 255.0 green:113 / 255.0 blue:255 / 255.0 alpha:1];
    }else
    {
        self.payBtn.userInteractionEnabled = NO;
        self.payBtn.backgroundColor = [UIColor colorWithRed:219 / 255.0 green:224 / 255.0 blue:255 / 255.0 alpha:1];
    }
}

//只允许输入小数点后2位
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    

    NSMutableString *futureString = [NSMutableString stringWithString:textField.text];
    [futureString insertString:string atIndex:range.location];
    
    NSInteger flag = 0;
    // 这个可以自定义,保留到小数点后两位,后几位都可以
    const NSInteger limited = 2;
    
    for (NSInteger i = futureString.length - 1; i >= 0; i--) {
        
        if ([futureString characterAtIndex:i] == '.') {
            // 如果大于了限制的就提示
            if (flag > limited) {
                
                NSLog(@"只能输入小数点后两位");
                return NO;
            }
            
            break;
        }
        
        flag++;
    }
    
    return YES;
}

-(void)delBtnAction
{
    if ([self.cv.visableTf.text length] == 0) {
        return ;
    }
    [self.cv.visableTf deleteBackward];
}
-(NSString*) removeLastValueFromAmount:(NSString*) amount{
    NSString* originalCentStr = [amount stringByReplacingOccurrencesOfString:@"." withString:@""];
    NSString* newCentStr = [originalCentStr substringToIndex:[originalCentStr length]-1];
    NSString* yuanStr = [self cent2Yuan:newCentStr];
    return yuanStr;
}
-(BOOL)inputRulesWithindepath:(NSIndexPath *)indexPath
{
    //如果输入了"点",再次输入"点"   或者第一位是"点"   禁止输入
    if ([self.cv.visableTf.text containsString:@"."] || self.cv.visableTf.text.length == 0) {
        if (indexPath.row == 9) {
            return NO;
        }
    }
    
    
    
    if ([self.cv.visableTf.text containsString:@"."]) {
        NSRange range1 = [self.cv.visableTf selectedRange1];
        NSRange range = [self.cv.visableTf.text rangeOfString:@"."];
        if (range1.location <= range.location) {
            //可以添加(获取到光标位置在小数点之前)
        }else
        {
            //如果小数点后面已经有了2位,禁止继续输入
            if ([self.cv.visableTf.text containsString:@"."]) {
                NSRange range = [self.cv.visableTf.text rangeOfString:@"."];
                NSMutableString *string = [NSMutableString stringWithString:self.cv.visableTf.text];
                [string insertString:[NSString stringWithFormat:@"%ld",indexPath.row] atIndex:range.location];
                NSInteger flag;
                if (indexPath.row == 11) {
                    flag = 2;
                }else
                {
                    flag = 1;
                }
                
                const NSInteger limited = 2;
                for (NSInteger i = string.length - 1; i  >= 0; i--) {
                    if ([string characterAtIndex:i] == '.') {
                        if (flag > limited) {
                            NSLog(@"小数点后两位了");
                            return NO;
                        }
                        break;
                    }
                    flag++;
                }
                //这里return Yes
                //        return YES;
                
            }
        }
    }
    
    
    
    //如果第一位是0  后面只能输入"点"
    if ([@"0" isEqualToString:self.cv.visableTf.text]) {
        if (indexPath.row != 9) {
            return NO;
        }
    }
    
    //不能直接输入”00“
    if (self.cv.visableTf.text.length == 0) {
        if (indexPath.row == 11) {
            return NO;
        }
    }
    return YES;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //cell点击高亮
    HNineCollectionViewCell *cell = (HNineCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = ETColorWithRGB(242, 244, 255);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(),
                   ^{
                       cell.backgroundColor = [UIColor whiteColor];
                   });
    
    //检测输入是否合法
    BOOL flag = [self inputRulesWithindepath:indexPath];
    if (!flag) {
        return;
    }
    
    
    if (self.cv.visableTf.text.length >= 10) {
        return;
    }

    if(self.cv.visableTf.text.length < 90){
        self.HoriginalAmount = [self.cv.visableTf text];
        self.HnewAccount = nil;
        switch (indexPath.row) {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
            case 8:
                   {
                     NSString *lastValueStr = [NSString stringWithFormat:@"%ld",indexPath.row + 1];
                       [self.cv.visableTf insertText:lastValueStr];
                     break;
            }
            case 9:
                   {
                       [self.cv.visableTf insertText:@"."];
                      break;
            }
            case 10:
            {
                
                if (self.cv.visableTf.text.length == 0) {
                    return;
                }
                
                [self.cv.visableTf insertText:@"0"];
                break;
            }
            case 11:
            {
                [self.cv.visableTf insertText:@"00"];
                break;
            }
            default:
                break;
        }
    }
}

-(NSString*) addLastValueToAmount:(NSString*) amount LastValue:(NSString*)value{
    NSString* originalCentStr = [amount stringByReplacingOccurrencesOfString:@"." withString:@""];
    NSString* newCentStr = [originalCentStr stringByAppendingString:value];
    NSString* yuanStr = [self cent2Yuan:newCentStr];
    return yuanStr;
}
-(NSString*)cent2Yuan:(NSString*)centStr{
    double cent = [centStr doubleValue];
    double yuan = cent/100;
    NSString*  yuanStr = [NSString stringWithFormat:@"%.2f",yuan];
    return yuanStr;
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.titleArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HNineCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.titleLab.text = self.titleArray[indexPath.row];
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}



-(void)collectCardBtnAction
{
    self.cv.backgroundView = [[UIView alloc] init];
    self.cv.backgroundView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
    AppDelegate *delegate  = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [delegate.window addSubview:self.cv.backgroundView];
    [self.cv.backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo([[UIScreen mainScreen]bounds].size.height);
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
    }];
    
    
    self.cv.arrowImv = [[UIImageView alloc] init];
    //投影
    self.cv.arrowImv.layer.shadowOpacity = 0.5;
    self.cv.arrowImv.layer.shadowRadius = 2;
    self.cv.arrowImv.layer.shadowOffset = CGSizeMake(0, 2);
    self.cv.arrowImv.layer.shadowColor = [UIColor colorWithRed:0 / 255.0 green:0 / 255.0 blue:0 / 255.0 alpha:1].CGColor;
    
    self.cv.arrowImv.image = [UIImage imageNamed:@"more_icon_arrow"];
    [self.cv.backgroundView addSubview:self.cv.arrowImv];
    [self.cv.arrowImv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.cv.collectCardBtn);
        make.top.equalTo(self.cv.collectCardBtn.mas_bottom).offset(3);
    }];
    
    self.cv.popCollectCardView = [[UIView alloc] init];
    self.cv.popCollectCardView.layer.cornerRadius = 8;
    //投影
    self.cv.popCollectCardView.layer.shadowOpacity = 0.5;
    self.cv.popCollectCardView.layer.shadowRadius = 2;
    self.cv.popCollectCardView.layer.shadowOffset = CGSizeMake(0, 3);
    self.cv.popCollectCardView.layer.shadowColor = [UIColor colorWithRed:0 / 255.0 green:0 / 255.0 blue:0 / 255.0 alpha:1].CGColor;
    
    self.cv.popCollectCardView.backgroundColor = [UIColor whiteColor];
    [self.cv.backgroundView addSubview:self.cv.popCollectCardView];
    [self.cv.popCollectCardView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.cv.collectCardLab.mas_right).offset(-5);
        make.top.equalTo(self.cv.arrowImv.mas_bottom);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(140);
    }];
    
    self.cv.tipsLab = [[UILabel alloc] init];
    self.cv.tipsLab.text = @"什么是收款卡?";
    self.cv.tipsLab.font = [UIFont boldSystemFontOfSize:14];
    self.cv.tipsLab.textColor = [UIColor colorWithRed:51 / 255.0 green:51 / 255.0 blue:51 / 255.0 alpha:1];
    [self.cv.popCollectCardView addSubview:self.cv.tipsLab];
    [self.cv.tipsLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(15);
        make.height.mas_equalTo(20);
    }];
    
    self.cv.tipsValueLab = [[UILabel alloc] init];
    self.cv.tipsValueLab.text = @"收款卡是用于信用卡收款或刷卡交易时，打款至您银行卡账户，必须绑定本人的银行储蓄卡！";
    self.cv.tipsValueLab.numberOfLines = 0;
    self.cv.tipsValueLab.font = [UIFont systemFontOfSize:13];
    self.cv.tipsValueLab.textColor = [UIColor colorWithRed:102 / 255.0 green:102 / 255.0 blue:102 / 255.0 alpha:1];
    [self.cv.popCollectCardView addSubview:self.cv.tipsValueLab];
    [self.cv.tipsValueLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.equalTo(self.cv.tipsLab.mas_bottom).offset(10);
        make.right.mas_equalTo(-15);
    }];
    
    
    
    //增加手势，点击空白处退出收款卡弹出View
    UITapGestureRecognizer *collectCardtap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(collectCardtapAction:)];
    [self.cv.backgroundView addGestureRecognizer:collectCardtap];
    
}

-(void)collectCardtapAction:(UITapGestureRecognizer*)gap
{
    /*
     CGPoint point = [gap locationInView:self.cv];
     point = [self.cv.popCollectCardView.layer convertPoint:point fromLayer:self.cv.backgroundView.layer];
     point.y = point.y +  ETTopHeight - ETTabBarArcHeight;
     if (!self.cv.backgroundView.hidden) {
     if (![self.cv.popCollectCardView.layer containsPoint:point]) {
     self.cv.backgroundView.hidden = YES;
     }
     }
     */
    
    CGPoint point = [gap locationInView:self.cv.backgroundView];
    point = [self.cv.popCollectCardView.layer convertPoint:point fromLayer:self.cv.backgroundView.layer];
    if (![self.cv.popCollectCardView.layer containsPoint:point]) {
        [self.cv.backgroundView removeFromSuperview];
    }
}


#pragma mark - collectionViewCell点击高亮


// 高亮时调用
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    HNineCollectionViewCell *cell = (HNineCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = ETColorWithRGB(242, 244, 255);
}

// 高亮结束调用
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    HNineCollectionViewCell *cell = (HNineCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
}

// 是否可以高亮
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}




@end
